#!/usr/bin/env python
# -*- coding: utf-8 -*-

# import numpy as np
# import matplotlib.pyplot as plt
# import os

import xlsxwriter

decode_info =[
((0, 8), 'Optype',  'optype', '==FFMA', 'FFMA'),
((8, 4), 'SType',  'stype', '==RRC', 'FFMA_RRC'),
((12, 3), 'Pred',  'pg', '=PT', 'FALU'),
((15, 1), 'PModi',  'pg.not', '=False', 'FALU'),
((16, 8), 'Reg',  'rd', '', 'FALU'),
((24, 8), 'Reg',  'ra', '', 'FALU'),
((32, 17), 'SImm17',  'vc.coffset', '', 'FFMA_RRC'),
((49, 5), 'UImm5',  'vc.cbank', '', 'FFMA_RRC'),
((62, 1), 'SignModi',  'vc.neg', '=False', 'FFMA_RRC'),
((63, 1), 'SignModi',  'vc.abs', '=False', 'FFMA_RRC'),
((64, 8), 'Reg',  'rb', '', 'FFMA_RRC'),
((72, 1), 'SignModi',  'ra.neg', '=False', 'FALU'),
((73, 1), 'SignModi',  'ra.abs', '=False', 'FALU'),
((74, 1), 'SignModi',  'rb.neg', '=False', 'FFMA_RRC'),
((75, 1), 'SignModi',  'rb.abs', '=False', 'FFMA_RRC'),
((76, 1), 'FPFtz',  'ftz', '=NoFTZ', 'FALU'),
((77, 1), 'FPSat',  'sat', '=NoSAT', 'FALU'),
((78, 2), 'FPRound',  'rnd', '=RN', 'FALU'),
((110, 6), 'ReqSB',  'req', '=0x0', 'ALL'),
((116, 3), 'WrSB',  'wsb', '=None', 'ALL'),
((119, 5), 'Sched',  'sched', '', 'ALL'),
((124, 1), 'Trap',  'trap', '=NoTrap', 'ALL'),
]

# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook("encoding.xlsx")
worksheet = workbook.add_worksheet()

# Increase the cell size of the merged cells to highlight the formatting.
worksheet.set_column(0, 0, 8)

# worksheet.set_column("B:D", 12)
# worksheet.set_row(3, 30)
# worksheet.set_row(6, 30)
# worksheet.set_row(7, 30)


# Create a format to use in the merged range.
vfmt = workbook.add_format(
    {
        "bold": 1,
        "border": 1,
        "align": "center",
        "valign": "vcenter",
    }
)

ufmt = workbook.add_format(
    {
        "bold": 1,
        "border": 0,
        "align": "center",
        "valign": "vcenter",
        "fg_color": "gray"
    }
)

HEADER_ROWS=2
for i in range(128):
    worksheet.write(i+HEADER_ROWS, 0, i, vfmt)

# ((124, 1), 'Trap',  'trap', '=NoTrap', 'ALL'),
for bfrange, bftype, bfvar, bfval, src in decode_info:
    if bfrange[1]>1:
        worksheet.merge_range(bfrange[0]+HEADER_ROWS, 1, bfrange[0]+bfrange[1]-1+HEADER_ROWS, 1, bfvar, vfmt)
    else:
        worksheet.write(bfrange[0]+HEADER_ROWS, 1, bfvar, vfmt)

set_bits = 0x1fffc0000000ffffc03fffffffffffff
for i in range(128):
    if set_bits & (1<<i) == 0:
        worksheet.write(HEADER_ROWS+i, 1, '', ufmt)

workbook.close()